﻿/*************************************************************************
* Copyright(c) 2020 - Present ShenZhen EscopeTech

* ShenZhen EscopeTech Incoperated CONFIDENTIAL
* ________________________________________________________________________
*
* [2020] - Present ShenZhen EscopeTech Incoperated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of ShenZhen EscopeTech Incoperated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to ShenZhen EscopeTech Incoperated
* and its suppliers and may be covered by P.R.C. and Foreign
Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior
written permission is obtained
* from ShenZhen EscopeTech Incoperated.
*
*************************************************************************/

#ifndef DLogInfoFormatBase_H
#define DLogInfoFormatBase_H

#include <QDebug>
#include <QString>
#include <QtGlobal>

#include "DTimeElapse.h"

#define qScopeInternal(no, type)                                       \
    DLogInfoFormatEnd type##no(QT_MESSAGELOG_FUNC, QT_MESSAGELOG_LINE, \
                               QT_MESSAGELOG_FILE, type);              \
    DLogInfoFormatBegin(QT_MESSAGELOG_FUNC, QT_MESSAGELOG_LINE,        \
                        QT_MESSAGELOG_FILE, type)

#define qDebugScopeR(no) qScopeInternal(no, QtDebugMsg)
#define qInfoScopeR(no) qScopeInternal(no, QtInfoMsg)
#define qWarningScopeR(no) qScopeInternal(no, QtWarningMsg)
#define qCriticalScopeR(no) qScopeInternal(no, QtCritical)

#define qDebugScope() qScopeInternal(0, QtDebugMsg)
#define qInfoScope() qScopeInternal(0, QtInfoMsg)
#define qWarningScope() qScopeInternal(0, QtWarningMsg)
#define qCriticalScope() qScopeInternal(0, QtCritical)

#define qErrorCode(c) qInfo() << QString("code error [%1]").arg(c)

class DLogInfoFormatBase
{
public:
    explicit DLogInfoFormatBase(const char* strMsgLogFunc,
                                const int& strMsgLogLine,
                                const char* strMsgLogFile,
                                const QtMsgType& msgType);
    virtual ~DLogInfoFormatBase() = default;

    template <typename T>
    DLogInfoFormatBase& operator<<(const T& t)
    {
        m_debug << t;
        return *this;
    }

    virtual void outputLog(const QString& strTag, const bool&& bLogTime);

protected:
    qint64 m_threadID = 0;
    QDebug m_debug;
    QString m_strArgs;
    QString m_strMsgLogFunc;
    int m_strMsgLogLine;
    QString m_strMsgLogFile;
    QtMsgType m_msgType = QtDebugMsg;
    DTimeElapse m_timeElapse;
};

class DLogInfoFormatBegin : public DLogInfoFormatBase
{
public:
    explicit DLogInfoFormatBegin(const char* strMsgLogFunc,
                                 const int& strMsgLogLine,
                                 const char* strMsgLogFile,
                                 const QtMsgType& msgType);
    virtual ~DLogInfoFormatBegin() override;
};

class DLogInfoFormatEnd : public DLogInfoFormatBase
{
public:
    explicit DLogInfoFormatEnd(const char* strMsgLogFunc,
                               const int& strMsgLogLine,
                               const char* strMsgLogFile,
                               const QtMsgType& msgType);
    virtual ~DLogInfoFormatEnd() override;
};

#endif  // DLogInfoFormatBase_H
